package controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import data.dao.UserDAOException;
import data.dto.UserDTO;
import data.dto.UserHierarchyDTO;
import data.dto.UserHierarchyDetailDTO;
import data.searchdto.UserHierarchyDetailSearchDTO;
import data.searchdto.UserHierarchySearchDTO;

import manager.UserManager;

/**
 * Servlet implementation class AdminManageHierarchy
 */
@WebServlet("/AdminManageHierarchy")
public class AdminManageHierarchy extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AdminManageHierarchy() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			doProcess(request, response);
		} catch (UserDAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			doProcess(request, response);
		} catch (UserDAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void doProcess(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException,
			UserDAOException {

		String page = "";
		UserDTO user = util.CurrentUser.getCurrentUser(request.getSession());

		// check user
		if (user == null) {
			page = util.Constants.homePage;
		} else if (!util.CurrentUser.isAdmin(user)) {
			page = util.Constants.homePage;
		} else {
			boolean load = request.getParameter("manage") == null ? false
					: true;
			UserManager userManager = new UserManager();

			if (load) {
				page = "/admin/manageHierarchy.jsp";
			} else {

				String type = request.getParameter("type") == null ? ""
						: request.getParameter("type").trim();

				page = "/AdminManageHierarchy?manage=true&name="
						+ request.getParameter("name") + "&supervisorusertype="
						+ request.getParameter("supervisorusertype");

				if (type.equalsIgnoreCase("manage")) {
					if (Short.parseShort(request.getParameter("usertype")) > 0) {
						UserHierarchyDTO userHierarchy = new UserHierarchyDTO();
						userHierarchy.setUserType(Short.parseShort(request
								.getParameter("supervisorusertype")));
						userHierarchy.setStatus(util.Constants.STATUS_SHOW);
						userManager.insertUserHierarchy(userHierarchy);

						userHierarchy = userManager
								.findUserHierarchy(new UserHierarchySearchDTO(
										Short.parseShort(request
												.getParameter("supervisorusertype"))));

						UserHierarchyDetailDTO userHierarchyDetail = new UserHierarchyDetailDTO();
						userHierarchyDetail.setHierarchyId(userHierarchy
								.getId());
						userHierarchyDetail.setUserType(Short
								.parseShort(request.getParameter("usertype")));

						userManager
								.insertUserHierarchyDetail(userHierarchyDetail);
					}
				} else if (type.equalsIgnoreCase("delete")) {
					UserHierarchyDTO userHierarchy = userManager
							.findUserHierarchy(new UserHierarchySearchDTO(
									Short.parseShort(request
											.getParameter("supervisorusertype"))));

					UserHierarchyDetailDTO userHierarchyDetail = new UserHierarchyDetailDTO();
					userHierarchyDetail.setHierarchyId(userHierarchy.getId());
					userHierarchyDetail.setUserType(Short.parseShort(request
							.getParameter("id")));

					userManager.deleteUserHierarchyDetail(userHierarchyDetail);
				} else {
					page = "/admin/ViewPage.jsp?type=hierarchy";
				}
			}

			request.setAttribute("userTypes",
					userManager.findAllUserTypeDisplay());

			// show subordinate
			if (request.getParameter("supervisorusertype") != null) {
				UserHierarchySearchDTO userHierarchySearch = new UserHierarchySearchDTO();
				userHierarchySearch.setUserType(Short.parseShort(request
						.getParameter("supervisorusertype")));

				UserHierarchyDTO userHierarchy = userManager
						.findUserHierarchy(userHierarchySearch);

				if (userHierarchy != null) {

					UserHierarchyDetailSearchDTO userHierarchyDetailSearch = new UserHierarchyDetailSearchDTO();
					userHierarchyDetailSearch.setHierarchyId(userHierarchy
							.getId());

					request.setAttribute(
							"subordinateUserTypes",
							userManager.findAllUserType(userManager
									.findAllHierarchyDetail(userHierarchyDetailSearch)));
				}
			}
		}

		RequestDispatcher rd = request.getRequestDispatcher(page);
		rd.forward(request, response);
	}

}
